package com.example.qxfw.slhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dmgis.entity.DmMapPoint;
import com.dmgis.weblib.MapCommon;
import com.example.qxfw.common.entity.zy.FireHistory;
import com.example.qxfw.slhx.mapper.FireHistoryMapper;
import com.example.qxfw.slhx.service.LshzglService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LshzglServiceImpl implements LshzglService {

    @Autowired
    private FireHistoryMapper fireHistoryMapper;
    @Override
    public List<FireHistory> getAllFireHistory() {
        return fireHistoryMapper.selectList(null);
    }

    @Override
    public FireHistory insertOneData(FireHistory fireHistory) {
        int insert = fireHistoryMapper.insert(fireHistory);
        if (insert > 0 ) {
            return fireHistory;
        }else {
            return null;
        }
    }

    @Override
    public FireHistory updateById(FireHistory fireHistory) {
        int update = fireHistoryMapper.update(fireHistory, new UpdateWrapper<FireHistory>()
                .eq("id", fireHistory.getId()));
        if (update > 0) {
            return fireHistory;
        }else {
            return null;
        }
    }

    @Override
    public List<FireHistory> attributeSearch(String firename, String address, String stationname, float areaStart,
            float areaEnd, int popthreatStart, int popthreatEnd, float propertythreatStart, float propertythreatEnd) {
        LambdaQueryWrapper<FireHistory> queryWrapper = new LambdaQueryWrapper<>();

        // 判断参数是否为空，如果不为空，则添加对应的查询条件
        if (StringUtils.isNotBlank(firename)) {
            queryWrapper.eq(FireHistory::getFirename, firename);
        }
        if (StringUtils.isNotBlank(address)) {
            queryWrapper.eq(FireHistory::getAddress, address);
        }
        if (StringUtils.isNotBlank(stationname)) {
            queryWrapper.eq(FireHistory::getStationname, stationname);
        }
        if (areaStart > 0 && areaEnd > 0) {
            queryWrapper.between(FireHistory::getDisasterarea, areaStart, areaEnd);
        }
        if (popthreatStart > 0 && popthreatEnd > 0) {
            queryWrapper.between(FireHistory::getPopthreat, popthreatStart, popthreatEnd);
        }
        if (propertythreatStart > 0 && propertythreatEnd > 0) {
            queryWrapper.between(FireHistory::getPropertythreat, propertythreatStart, propertythreatEnd);
        }
        return fireHistoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<FireHistory> conditionalQuery(String[][] pos) {
        System.out.println("参数："+pos);
        //多边形
        List<DmMapPoint> coordinatesList = new ArrayList<>();
        for (String[] point : pos) {
            coordinatesList.add(new DmMapPoint(Double.valueOf(point[0]), Double.valueOf(point[1])));
        }
        //坐标点
        List<FireHistory> fireHistories = fireHistoryMapper.selectList(null);

        //在多边形范围内点
        List<FireHistory> result = new ArrayList<>();
        for (FireHistory fireHistory : fireHistories) {
            boolean b = MapCommon.pointWithinPolygon(coordinatesList, fireHistory.getLongitude(), fireHistory.getLatitude());
            if (b){
                result.add(fireHistory);
            }
        }
        return result;
    }

    @Override
    public int deleteById(int id) {
        return fireHistoryMapper.deleteById(id);
    }
}
